import config from './config.js'
import commonJS from './common.js'
import store from '../store/index.js'

const httpRequest = function(url, method, data, loading) {
	return new Promise((resolve, reject) => {
		if (loading) {
			uni.showLoading({
				title: 'Loading'
			})
		}
		let token = commonJS.getToken() || '';
		let header = {
			'accessToken': token,
			'content-type': 'application/json',
		}
		uni.request({
			url: config.base_url + url,
			method: method || 'get',
			header: header,
			data: data,
			success: (res) => {
				console.log(res);
				uni.hideLoading()
				if (res.statusCode === 200) {
					if (res.data.returnCode === '6008') {
						uni.showToast({
							title: '当前用户信息已过期，请重新登录。',
							icon: 'none',
						})
						console.log('1111')
						store.commit('app/LOGOUT');
						store.commit('user/DEL_USERINFO');
						console.log('222')
						uni.switchTab({
							url: '/pages/tabbar/mine/index'
						})
					} else {
						resolve(res.data)
					}
				} else {
					uni.showToast({
						title: '请检查你的网络链接',
						icon: 'none'
					})
				}
			},
			fail: (err) => {
				console.log(url);
				console.log(err)
				uni.hideLoading()
				uni.showToast({
					title: err.errMsg,
					icon: "none"
				})
			}
		})
	})

}
export default httpRequest;
